package com.mata.worktime.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mata.worktime.constant.UserConstants;
import com.mata.worktime.service.user.DeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mata.worktime.dao.user.DeptMapper;
import com.mata.worktime.dao.user.UserMapper;
import com.mata.worktime.dao.work.ProjectMapper;
import com.mata.worktime.entity.user.Dept;
import com.mata.worktime.entity.user.User;
import com.mata.worktime.entity.work.Project;

import java.util.*;


/**
 * @author earthchen
 * @date 2018/10/8
 **/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService
{
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Dept> getParentDept()
    {
        return deptMapper.getParentDept();
    }

    /**
     * 获得子部门
     *
     * @return
     */
    @Override
    public List<Dept> getChildrenDept(String parentId)
    {
        return deptMapper.getChildrenDept(parentId);
    }

    @Override
    public List<Dept> getAllChildrenDept(String parentId) {
        return deptMapper.getAllChildrenDept(parentId);
    }

    @Override
    public Dept selectDeptById(Long deptId)
    {
        return deptMapper.selectDeptById(deptId);
    }

    @Override
    public List<Map<String, Object>> selectDeptTree(Project project, User user)
    {
        List<String> deptIdList = null;
        if (null != user.getId())
        {
            String userId = user.getId().toString();
            user = userMapper.selectById(userId);
            String deptId = user.getDeptId();
            String[] deptIds = deptId.split(",");
            deptIdList = new ArrayList<>(deptIds.length);
            Collections.addAll(deptIdList, deptIds);
        }
        if (StringUtils.isNotBlank(project.getProjectId()))
        {
            String projectId = project.getProjectId();
            project = projectMapper.selectById(projectId);
            String deptId = project.getDeptId();
            String[] deptIds = deptId.split(",");
            deptIdList = new ArrayList<>(deptIds.length);
            Collections.addAll(deptIdList, deptIds);
        }
        List<Map<String, Object>> trees = new ArrayList<>();
        List<Dept> deptList = list(new QueryWrapper<>());
        for (Dept dept : deptList)
        {
            if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
            {
                Map<String, Object> deptMap = new HashMap<>();
                deptMap.put("id", dept.getId().toString());
                deptMap.put("pId", dept.getParentId().toString());
                deptMap.put("name", dept.getDeptName());
                deptMap.put("title", dept.getDeptName());
                if (project.getDeptId() != null||user.getDeptId()!=null)
                {
                    deptMap.put("checked", deptIdList.contains(dept.getId().toString()));
                }
                else
                {
                    deptMap.put("checked", false);
                }

                trees.add(deptMap);
            }
        }
        return trees;
    }

    @Override
    public List<User> checkIfExistUser(String deptId)
    {
        return deptMapper.checkIfExistUser(deptId);
    }

    @Override
    public int insertByList(List<Dept> insertList) {
        return deptMapper.insertByList(insertList);
    }

    @Override
    public int delDeptByIds(List<Long> delDeptList) {
        return deptMapper.deleteBatchIds(delDeptList);
    }

    @Override
    public List<Dept> getAllDept() {
        return deptMapper.getAllDept();
    }
}
